package com.ruoyi.web.controller.dish;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.dish.domain.DishMenu;
import com.ruoyi.dish.domain.DishOrder;
import com.ruoyi.dish.domain.vo.OrderVo;
import com.ruoyi.dish.service.DishMenuService;
import com.ruoyi.dish.service.DishOrderService;
import com.ruoyi.human.domain.HuRider;
import com.ruoyi.human.domain.HuStore;
import com.ruoyi.human.service.HuRiderService;
import com.ruoyi.human.service.HuStoreService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DishController extends BaseController {
    @Autowired
    private DishOrderService dishOrderService;
    @Autowired
    private DishMenuService dishMenuService;
    @Autowired
    private HuStoreService huStoreService;
    @Autowired
    private HuRiderService huRiderService;
    @PostMapping("/dish/add")
    public AjaxResult addDish(@RequestBody OrderVo orderVo) {
        DishOrder dishOrder = new DishOrder();
        //获取登录者的信息作为客户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        dishOrder.setUserId(loginUser.getUserId());
        //复制信息
        BeanUtil.copyProperties(orderVo, dishOrder);
        //拉取菜单id,商店id，骑手id
        String menuId = orderVo.getMenuId();
        int storeId = orderVo.getStoreId();
        int riderId = orderVo.getRiderId();
        //根据菜单id查询菜单信息
        DishMenu menu = dishMenuService.getMenuById(menuId);
        //根据商店id查询商店信息
        HuStore store = huStoreService.getStoreById(storeId);
        //根据骑手id查询骑手信息
        HuRider rider = huRiderService.getRiderById(riderId);
        //在获取到了登录者的id之后去查找这个登陆者的名字和电话
        ISysUserService sysUserService = SpringUtils.getBean(ISysUserService.class);
        SysUser sysUser = sysUserService.selectUserById(loginUser.getUserId());
        //添加商店信息
        dishOrder.setStoreName(store.getStoreName());
        dishOrder.setStoreAddress(store.getStoreAddress());
        //添加骑手信息
        dishOrder.setRiderName(rider.getRiderName());
        dishOrder.setRiderPhone(rider.getRiderPhone());
        //添加菜品信息
        dishOrder.setDishName(menu.getDishName());
        dishOrder.setUserName(sysUser.getUserName());
        dishOrder.setPhonenumber(sysUser.getPhonenumber());
        dishOrder.setOrderStatus("未接单");
        dishOrder.setCreateTime(DateUtils.getNowDate());
        //添加订单信息
        dishOrderService.insertOrder(dishOrder);
        return AjaxResult.success();
    }
    //显示订单
    @PostMapping("/dish/order/list")
    public TableDataInfo listOrders() {
        startPage(); // 设置分页信息
        List<DishOrder> orderList = dishOrderService.selectOrderList();
        return getDataTable(orderList);
    }
}
